package com.cdatos.golGana.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.cdatos.golGana.excepcion.LoginException;
import com.cdatos.golGana.model.Usuario;
import com.cdatos.golGana.providers.SortableGenericsDataProvider;
import com.cdatos.golGana.service.UsuarioService;
import com.cdatos.golGana.session.GolGanaSession;

public class MainPage extends WebPage {

	@SpringBean(name="usuarioService")
	private UsuarioService uService;
	
	public MainPage() {
		super();
		add(new Label("tituloPagina", new PropertyModel<String>(this,"tituloPagina")));
		final FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);
		add(new SignInForm("signInForm", new Model<Usuario>()));
		add(getTablaPosiciones());
 	}
	
	public String getTituloPagina(){
		return ".::Gol Gana!::.";
	}

	@SuppressWarnings("all")
	public final class SignInForm extends Form {

		public SignInForm(String id, IModel<Usuario> model) {
			super(id, model);
			model.setObject(new Usuario());
			RequiredTextField<String> txtNick = new RequiredTextField<String>(
					"nick", new PropertyModel<String>(model, "nick"));
			add(txtNick);
			
			PasswordTextField txtPass = new PasswordTextField("contrasenia",
					new PropertyModel<String>(model, "contrasenia"));
			add(txtPass);
		}
		
		@Override
		@SuppressWarnings("all")
		protected void onSubmit() {
			IModel<Usuario> model = getModel();
    		Usuario usuarioPage = model.getObject();
    		GolGanaSession session = (GolGanaSession)getSession();
			try {
				Usuario usuario = uService.login(usuarioPage.getNick(), usuarioPage.getContrasenia());
				session.setUsuario(usuario);
				setResponsePage(HomePage.class);			
			} catch (LoginException e) {
				error(e.getMessage());
			}catch (Exception e) {
				error(e.getMessage());
			}
		}
	}
	
//	private AjaxFallbackDefaultDataTable<Usuario> getTablaPosiciones(){
	private DefaultDataTable<Usuario> getTablaPosiciones(){
	    List<Usuario> usuarios = uService.getAll();

		List<IColumn<Usuario>> columns = new ArrayList<IColumn<Usuario>>();
	    columns.add(new PropertyColumn<Usuario>(new Model<String>("Nick"), "nick"));
	    columns.add(new PropertyColumn<Usuario>(new Model<String>("Puntos"), "puntos"));
//	    return new AjaxFallbackDefaultDataTable<Usuario>("table", columns,new SortableGenericsDataProvider(usuarios,"puntos",false), 2);
	    return new DefaultDataTable<Usuario>("table",columns, new SortableGenericsDataProvider<Usuario>(usuarios,"puntos",false),8);
	}
	
}
